The Code Project
View our advertisers Advertise with us
All Topics, ASP.NET >> VB / VBScript >> Unedited Reader Contributions

MCL Global Hotkey ActiveX control
By Duncan Edwards Jones

A system wide hotkey control written in Vb6 
  VBScript, VB
Windows (WinXP, Win2K, Win95, NT4, Win98, WinME)
Win32, VS (VS5)
Dev
  Posted 13 May 2003
Articles by this author
26,494 views
Note: This is an unedited reader contribution [Modify this article] [Delete this article]
Search:
Ultimate Combo
MFC tools $399
Send to a friend
My Profile
My Settings
My Articles
My Bookmarks
Sign out
 


Print Broken Article? Bookmark Discuss
3 votes for this article.
Popularity: 1.67. Rating: 3.5 out of 5.
  • You are signed up for one or more newsletters but unfortunately we are unable to send you emails. Please click here to have an email sent that will allow us to confirm your email address.

Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report this article.

Introduction

The attached source code is for a system-wide global hotkey control written in Visual Basic 6, orginally posted on the Merrion Computing Downloads

Registering a hotkey

There are two API calls concerned with registering a system wide hotkey: RegisterHotkey abd UnregisterHotkey - the declaratiosn are:

Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long

To register the hotkey you need to specify hwnd - the window which will be notified when the hotkey combination is pressed, id - the unique identifier for the hotkey, fsModifiers - the modifiers of the hotkey (i.e. whether ALT, SHIFT, CTRL or WIN keys are pressed) and finally vKey - the virtual key code for the hot key. The modifiers which can be combined together are:

Public Enum enHotkeyModifiers
    MOD_ALT = &H1
    MOD_CONTROL = &H2
    MOD_SHIFT = &H4
    MOD_WIN = &H8
End Enum

Listening out for the hotkey

When a hotkey which has been registered successfully is pressed, the window specified in the hwnd is posted a WM_HOTKEY registered window message. Additionally the lParam parameter holds the modifier and virtual key code in it's high and low words respectively

Public Function VB_WindowProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Dim VKey As Long
Dim Modifier As Long

If wMsg = WM_HOTKEY Then
    VKey = HiWord(lParam)
    Modifier = LoWord(lParam)
    '\\ Raise an event here to resppond to the hotkey
End If

If sWindow.OldWndProc = 0 Then
    VB_WindowProc = DefWindowProc(hwnd, wMsg, wParam, lParam)
Else
    VB_WindowProc = CallWindowProc(sWindow.OldWndProc, hwnd, wMsg, wParam, lParam)
End If

End Function

Visual Basic does not handle the WM_HOTKEY message by default which means that you need to subclass the window hwnd in order to respond to it. To do this you use the API call SetWindowLong with the index GWL_WNDPROC to replace the existing window procedure with your one.

'\\ API Calls used in subclassing windows....
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'\\ Window specific information
Private Declare Function GetWindowLongApi Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLongApi Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC = (-4)

Using the control in your application

To use this control in a Visual basic application, compile the attached source code and then add an instance of the resulting OCX to your project. Add an instance of the MCLHotkey control to your form and press F4 to set the properties. Then add the code that you want executed when the hotkey is pressed to the HotkeyPressed event:

Private Sub MCLHotkey1_HotkeyPressed()
  Debug.Print "You pressed the hotkey at " & Now
End Sub

About Duncan Edwards Jones


Contract VB6 / SQL server developer moving over to VB.Net

Click here to view Duncan Edwards Jones's online profile.


Other popular articles:

[Top] Rate this Article for us!     PoorExcellent  

FAQ  Noise tolerance    Search comments  
  View    Per page  
New Message Msgs 1 to 8 of 8 (Total: 8) (Refresh) First Prev Next     
Subject  Author  Date 
  Excellent Control but a couple of problems   AvoSoftwareTeam  8:38 29 Oct '05 
  Re: Excellent Control but a couple of problems   Duncan Edwards Jones  12:32 29 Oct '05 
  Disable the [Ctrl]+[Esc] keys Unconfirmed/Anonymous posting  Anonymous  17:46 13 Jan '05 
  Great but...   Q_HAL  10:36 2 Aug '04 
  Nice Ariticle:reason why I rated this excellent   Ipog Gates  6:45 18 Jan '04 
  .NET Version   Merrion  6:45 13 Jun '03 
  Re: .NET Version   Jehosephat  16:34 22 Nov '04 
  Re: .NET Version   Merrion  18:14 22 Nov '04 
Last Visit: 18:37 Friday 24th March, 2006 First Prev Next     

General comment    News / Info    Question    Answer    Joke / Game    Admin message


All Topics, ASP.NET >> VB / VBScript >> Unedited Reader Contributions
Updated: 13 May 2003
Article content copyright Duncan Edwards Jones, 2003
everything else Copyright © CodeProject, 1999-2006.
Web02 | Advertise on The Code Project | Privacy

The Ultimate Toolbox • ASP Alliance • Developer Fusion • Developersdex • DevGuru • Programmers Heaven • Planet Source Code • Tek-Tips Forums •